<template>
  <div class='container'>
    <el-card>
  <div style="margin-bottom:10px">
       <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
  </div>
  <el-form :inline="true" class="demo-form-inline">
        <el-row type="flex" class="row-bg" justify="space-between">
          <el-col :span="12">
  <el-form-item label="目录名称">
    <el-input v-model="formList.directoryName"></el-input>
  </el-form-item>
  <el-form-item label="状态" style="padding-left:20px">
    <el-select placeholder="请选择" v-model="formList.state">
      <el-option label="启用" :value="1" ></el-option>
      <el-option label="禁用" :value="0"></el-option>
    </el-select>
    <el-button @click="empty" style="margin-left:10px">清除</el-button>
    <el-button type="primary" @click="search">搜索</el-button>
  </el-form-item>
  </el-col>
  <el-col :span="4">
  <el-form-item>
    <el-button icon="el-icon-back" type="text" @click="$router.push('list')">返回学科</el-button>
    <el-button type="success" icon="el-icon-edit" @click="openDialog()">新增目录</el-button>
  </el-form-item>
  </el-col>
</el-row>
</el-form>

<el-alert
    :title="`数据一共 ${total} 条`"
    style="margin-bottom:15px"
    type="info"
    show-icon>
  </el-alert>

  <!-- 列表 -->
  <el-table
      :header-cell-style="{background:'#fafafa'}"
      style="width: 100%"
      :data="list"
      >
      <el-table-column
      class="title"
      type="index"
      :index="indexMethod"
       label="序号"
      width="80">
    </el-table-column>
      <el-table-column
      class="title"
        prop="subjectName"
        label="所属学科"
        width="180">
      </el-table-column>
      <el-table-column
      class="title"
        prop="directoryName"
        label="目录名称"
        width="180">
      </el-table-column>
      <el-table-column
      class="title"
        prop="username"
        label="创建者">
      </el-table-column>
       <el-table-column
       class="title"
       prop="addDate"
        label="创建日期">
        <template #default="{row}">
          {{row.addDate | parseTimeByString}}
        </template>
      </el-table-column>
       <el-table-column
       class="title"
        prop="totals"
        label="面试题数量">
      </el-table-column>
       <el-table-column
       class="title"
       prop="state"
       :formatter="state"
        label="状态">
      </el-table-column>
       <el-table-column label="操作" class="title">
         <template #default="{row}">
         <el-button type="text" @click="toggleState(row)">{{row.state===1?'禁用':'启用'}}</el-button>
         <el-button type="text" @click="openDialog(row)" :disabled="row.state===1">修改</el-button>
         <el-button type="text" @click="del(row)" :disabled="row.state===1||row.totals > 0">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <!--
      page-size 每页显示条目个数
      total 总数
      page-sizes每页显示个数选择器的选择设置
      current-page 当前页数
      size-change pageSize改变时会触发
      current-change currentPage改变时会触发
     -->
    <div style="margin-top:20px;text-align:right" >
      <el-pagination
      background
      hide-on-single-page
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formList.page"
      :page-sizes="[2,4,6,8,10]"
      :page-size="formList.pagesize"
      layout="prev, pager, next,sizes, jumper"
      :total="total">
    </el-pagination>
    </div>

    </el-card>
     <directorys-add ref="directorysAdd" :data="currDirectory" @updateList="updateList"></directorys-add>
    </div>
</template>

<script>
import { status } from '@/api/hmmm/constants.js'
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import DirectorysAdd from '../components/directorys-add.vue'
export default {
  name: 'DirectorysList',
  components: { DirectorysAdd },
  data () {
    return {
      formList: {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      list: [],
      total: 0,
      currDirectory: {}

    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 渲染页面
    async getList () {
      // this.formList.subjectId = this.subject.id || null
      const { data } = await list(this.formList)
      // console.log(data)
      this.list = data.items
      this.total = data.counts
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.formList.pagesize = val
      this.formList.page = 1
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.formList.page = val
      this.getList()
    },
    indexMethod (index) {
      // index 从0开始
      const res = index + 1 + this.formList.pagesize * (this.formList.page - 1)
      // console.log(res, 1)
      return res
    },
    // 清除输入框
    empty () {
      this.formList = {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
      this.getList()
    },
    // 搜索
    search () {
      this.formList.page = 1
      this.getList()
    },
    // 状态
    state (row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const arr = status
      // console.log(arr)
      const res = arr.find(item => item.value === cellValue)
      // console.log(res)
      return res ? res.label : '未知'
    },
    async toggleState (data) {
      await changeState({
        id: data.id,
        state: data.state === 1 ? 0 : 1
      })
      this.$message.success('切换状态成功')
      this.list.state = this.list.state === 1 ? 0 : 1
      this.getList()
      // console.log(this.list.state, 9999)
    },
    // 修改
    openDialog (directory = {}) {
      this.currDirectory = directory
      this.$nextTick(() => {
        this.$refs.directorysAdd.open()
      })
    },
    updateList () {
      if (!this.currDirectory.id) {
        this.formList.page = 1
      }
      this.getList()
    },
    // 删除
    async del (data) {
      await this.$confirm('你确定要删除吗', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(data)
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {
        console.log('取消')
      })
    }

  }
}
</script>

<style scoped lang='scss'>
.el-table thead {
  color:#909399;
   border-bottom:2px solid #e8e8e8;
}

</style>
